<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class AdminauthController extends BaseController {

//==========================权限组管理=======================================

    //权限组列表
    public function authgroup(){
		$count = M('auth_group')->count();
        $page = getpage($count,10);
    	$authgroup_arr = M('auth_group')->where('id != 1')->limit($page->firstRow, $page->listRows)->select();
    	$this->assign('authgroup_arr',$authgroup_arr);
        $this->assign('page', $page->show());
    	$this->display();
    }

    //权限组详情
    public function authgroup_details(){
    	$data['id'] = I('get.id');
    	$authgroup = M('auth_group')->where($data)->find();
    	//权限信息
        $auth_arr = M('auth_rule')->field('id,title')->order('category')->select();
        $count = M('auth_rule')->count('id');
        $cols = ceil($count/5);
        $rule_arr = explode(',', $authgroup['rules']);

    	$this->assign('authgroup',$authgroup);
        $this->assign('auth_arr',$auth_arr);
        $this->assign('cols',$cols);
        $this->assign('rule_arr',$rule_arr);
    	$this->display();
    }

    //添加权限组
    public function authgroup_add(){
    	//添加处理
    	if (I('post.action') == 'authgroup_add_action') {
            if ($_POST != array_filter($_POST)) {
                $this->error('选项不能有空值！');
            }
    		$data['title'] = trim($_POST['title']);
    		if (M('auth_group')->where($data)->find()) {
    			$this->error('权限组名称已存在，请重新添加！');
    		}
    		unset($_POST['action']);
    		$_POST['title'] = trim($_POST['title']);
    		$_POST['rules'] = implode(',', $_POST['rules']);
    		if (M('auth_group')->add($_POST)) {
    			$this->success('权限组添加成功！','authgroup');
    		}else{
    			$this->error('权限组添加失败！');
    		}
    		exit();
    	}

    	//权限信息
        $auth_arr = M('auth_rule')->field('id,title')->order('category')->select();
        $count = M('auth_rule')->count('id');
        $cols = ceil($count/5);
        $rule_arr = explode(',', $authgroup['rules']);

        $this->assign('auth_arr',$auth_arr);
        $this->assign('cols',$cols);
        $this->assign('rule_arr',$rule_arr);
    	$this->display();
    }

    //修改权限组
    public function authgroup_edit(){
    	//修改处理
    	if (I('post.action') == 'authgroup_edit_action') {
    		unset($_POST['action']);
    		$_POST['title'] = trim($_POST['title']);
    		$_POST['rules'] = implode(',', $_POST['rules']);
    		if (M('auth_group')->save($_POST)) {
    			$this->success('权限组修改成功！','authgroup');
    		}else{
    			$this->error('权限组修改失败！');
    		}
    		exit();
    	}

    	//权限信息
    	$data['id'] = I('get.id');
    	$authgroup_arr = M('auth_group')->where($data)->find();
        $auth_arr = M('auth_rule')->field('id,title')->order('category')->select();
        $count = M('auth_rule')->count('id');
        $cols = ceil($count/5);
        $rule_arr = explode(',', $authgroup_arr['rules']);

        $this->assign('authgroup_arr',$authgroup_arr);
        $this->assign('auth_arr',$auth_arr);
        $this->assign('cols',$cols);
        $this->assign('rule_arr',$rule_arr);
    	$this->display();
    }


//========================管理员操作======================================

	public function admin(){

        $count = M('admin')->where($data)->count();
        $page = getpage($count,10);

        $admininfo = M('admin')->alias('a')->field('admin_id,admin_name,admin_phone,admin_avatar,title,rules')->join('left join midi_auth_group_access as ga on a.admin_id = ga.uid')->join('left join midi_auth_group as g on ga.group_id = g.id')->order('admin_id asc')->where('a.admin_id != 1')->limit($page->firstRow, $page->listRows)->select();

        $this->assign('admininfo',$admininfo);
        $this->assign('page', $page->show());
        $this->display();
    }

    public function admin_details(){
    	$data['admin_id'] = I('get.admin_id');
    	$admininfo = M('admin')->alias('a')->field('admin_id,admin_name,admin_phone,admin_avatar,title,rules')->join('left join midi_auth_group_access as ga on a.admin_id = ga.uid')->join('left join midi_auth_group as g on ga.group_id = g.id')->where($data)->find();
    	//权限信息
        $auth_arr = M('auth_rule')->field('id,title')->order('category')->select();
        $count = M('auth_rule')->count('id');
        $cols = ceil($count/5);
        $rule_arr = explode(',', $admininfo['rules']);

    	$this->assign('admininfo',$admininfo);
        $this->assign('auth_arr',$auth_arr);
        $this->assign('cols',$cols);
        $this->assign('rule_arr',$rule_arr);

        $this->display();
    }

    public function admin_add(){
    	if (I('post.action') == 'admin_add_action') {
    		$data['admin_name'] = trim($_POST['admin_name']);
    		if (M('admin')->where($data)->find()) {
    			$this->error('管理员账号已存在，请重新添加！');
    		}
    		unset($_POST['action']);
    		if ($_POST != array_filter($_POST)) {
    			$this->error('选项中不能有空值！');
    		}
    		if ($_POST['admin_password'] != $_POST['admin_repassword']) {
    			$this->error('两次密码输入不一致！');
    		}
    		$data['admin_password'] = md5($_POST['admin_password'].C(AES_KEY));
    		if ($uid = M('admin')->add($data)) {
    			$where['uid'] = $uid;
    			$where['group_id'] = $_POST['group_id'];
    			M('auth_group_access')->add($where);
    			$this->success('管理员添加成功！','admin');
    		}else{
    			$this->error('管理员添加失败！');
    		}
    		exit();
    	}
    	//权限信息
        $auth_arr = M('auth_group')->field('id,title')->where('id != 1')->select();
        $this->assign('auth_arr',$auth_arr);
    	$this->display();
    }

    public function admin_edit(){
    	//修改管理信息
    	if (I('post.action') == 'admin_edit_action') {
    		unset($_POST['action']);
            $finfo = upload($_FILES['avatar']);
            if ($finfo['status'] == 0 && $finfo['msg'] != 'nofile') {
                $this->error($finfo['msg']);
            } elseif ($finfo['status'] == 1) {
                $_POST['admin_avatar'] = $finfo['filepath'];
            }

            if(M('admin')->save($_POST)){
                $this->success('修改信息成功');
            }else{
                $this->error('没有信息被修改');
            }
            exit();
    	}else if (I('post.action') == 'changepwd_action') {//重置密码
    		if (empty($_POST['admin_password'])) {
    			$this->error('密码不能为空！');
    		}
    		unset($_POST['action']);
    		$_POST['admin_password'] = md5($_POST['admin_password'].C(AES_KEY));
    		M('admin')->save($_POST);
    		$this->success('重置密码成功！','admin');
    		exit();
    	}
    	$data['admin_id'] = I('get.admin_id');
    	$admininfo = M('admin')->alias('a')->field('admin_id,admin_name,admin_password,admin_phone,admin_avatar,title,rules')->join('left join midi_auth_group_access as ga on a.admin_id = ga.uid')->join('left join midi_auth_group as g on ga.group_id = g.id')->where($data)->find();
    	$this->assign('admininfo',$admininfo);
    	$this->display();
    }

    public function admin_del(){
    	$data['admin_id'] = I('get.admin_id');
    	if (M('admin')->where($data)->delete()) {
    		$this->success('删除管理员成功！');
    	}else{
    		$this->error('删除管理员失败！');
    	}
    }


}